package cn.yjr.vectori.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import cn.yjr.fragment.ChatRoomFragment;
import cn.yjr.vector.R;
import cn.yjr.vector.tools.BitmapHelper;
import cn.yjr.vector.tools.YJRApplication;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author vector
 *
 */
public class ChatRoomAdapter extends BaseAdapter{

	Context context;
	LayoutInflater inflater;
	JSONArray patients;
	JSONObject patient;
	String consultStatus = null;
	Long deadline = (long) 0;
	boolean sendable = false;
	String id = null;
	String name =null;
	String head = null;
	ImageLoader imageLoader;
	DisplayImageOptions options;
	public ChatRoomAdapter(Context context,ImageLoader imageLoader, DisplayImageOptions options) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.imageLoader = imageLoader;
		this.options = options;
		patients = ChatRoomFragment.patients;
		//Log.v("liuyi",doctors.toString());
		
	}

	@Override
	public int getCount() {
		
		//return doctors.length();
		return patients.length();
	}

	@Override
	public Object getItem(int position) {
		try {
			return patients.getJSONObject(position);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.patient_item, parent,false);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.left_tv= (TextView) convertView.findViewById(R.id.left_tv);
			holder.time_tv= (TextView) convertView.findViewById(R.id.time_tv);
			holder.last_msg_tv= (TextView) convertView.findViewById(R.id.last_msg_tv);
			holder.patient_head_imv = (ImageView) convertView.findViewById(R.id.patient_head_imv);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			
			JSONObject patient_ = patients.getJSONObject(position);
			consultStatus = patient_.getString("consultStatus");
			deadline = patient_.getLong("deadline");
			patient= patient_.getJSONObject("patient");
			
			sendable = patient_.getBoolean("sendable");
			name = patient.getString("name");
			head = patient.getString("avatar");
			id = patient.getString("id");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		holder.name_tv.setText(name );
		holder.time_tv.setText("11:20");
		if(consultStatus.equals("N")){
			holder.last_msg_tv.setText("[咨询结束]");
			holder.last_msg_tv.setTextColor(context.getResources().getColor(R.color.red_font));

		}else if(consultStatus.equals("F")){
			holder.last_msg_tv.setText("[已结束]");
		}else if(consultStatus.equals("C")){
			
		}else if(consultStatus.equals("A")){
			holder.last_msg_tv.setText("[申请咨询中 剩余24小时00分钟]");
			holder.last_msg_tv.setTextColor(context.getResources().getColor(R.color.red_font));
		}else{
			
		}
		
		//holder.doctor_head_imv.setImageBitmap(BitmapHelper.toRoundBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.head)));
		Log.v("weiyi",YJRApplication.head_path+head);
		//imageLoader.displayImage(YJRApplication.head_path+head, holder.patient_head_imv, options);
		imageLoader.loadImage(YJRApplication.head_path+head,options, new ImageLoadingListener(){

			@Override
			public void onLoadingCancelled(String arg0, View arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
				// TODO Auto-generated method stub
				holder.patient_head_imv.setImageBitmap(BitmapHelper.toRoundBitmap(arg2));
				//head_imv.setImageBitmap(arg2);
			}

			@Override
			public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onLoadingStarted(String arg0, View arg1) {
				// TODO Auto-generated method stub
				
			}

			
		});
		return convertView;
	}

    class ViewHolder {
        ImageView patient_head_imv;
        TextView name_tv,left_tv,time_tv,last_msg_tv;
    }
	
}
